'use strict';

const Controller = require('egg').Controller;

class LoginController extends Controller {
  async index() {
    let {
      ctx,
      app,
      config
    } = this;
    let user = ctx.request.body;
    let sQuser = await ctx.service.login.index(user);
    if (sQuser.status === 0) {
      ctx.body = config.sendJson({
        code: config.code.MESSAGE,
        message: sQuser.msg
      });
    } else {
      const token = app.jwt.sign({
        y_id: sQuser.data.y_id,
        username: sQuser.data.username,
      }, app.config.jwt.secret, {
        expiresIn: '15m',
      });
      ctx.set({
        authorization: token
      });
      ctx.body = config.sendJson({});
    }
  }
}

module.exports = LoginController;